package com.aistar.service.impl;

import com.aistar.mapper.ProductMapper;
import com.aistar.mapper.SeckillMapper;
import com.aistar.pojo.*;
import com.aistar.service.ProductService;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SeckillMapper seckillMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ServerResponse getById(Integer id) {
        ProductExample example = new ProductExample();
        example.createCriteria().andStatusEqualTo("1").andIdEqualTo(id);
        List<ProductWithBLOBs> productList = productMapper.selectByExampleWithBLOBs(example);
        Product product = productList.get(0);
        if (product == null)
            return ServerResponse.getDataFailed("没有查询到数据！");
        else
            return ServerResponse.getDataSuccess(product);
    }

    @Override
    public ServerResponse getHotProduct() {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        example.setOrderByClause("review_count desc");
        List<Product> hotPproductList = productMapper.selectByExample(example);

        if (hotPproductList != null & hotPproductList.size() > 0) {
            return ServerResponse.getDataSuccess(hotPproductList);
        } else {
            return ServerResponse.getDataFailed("获取数据失败");
        }
    }

    @Override
    public ServerResponse getRimProduct(String site) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        criteria.andStartCityEqualTo(site);
        List<Product> rimProductList = productMapper.selectByExample(example);

        if (rimProductList != null & rimProductList.size() > 0) {
            return ServerResponse.getDataSuccess(rimProductList);
        } else {
            return ServerResponse.getDataFailed("获取数据失败");
        }
    }

    /*
     * 天数超过3的产品
     * */
    @Override
    public ServerResponse getLongProduct() {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        criteria.andProductDaysGreaterThan("3");
        List<Product> longProductList = productMapper.selectByExample(example);
        if (longProductList != null & longProductList.size() > 0)
            return ServerResponse.getDataSuccess(longProductList);
        else
            return ServerResponse.getDataFailed("获取数据失败");
    }

    @Override
    public ServerResponse getAllSearch(Integer pageNum, String keyword, String method,
                                       Integer days, String startCity, String feature,
                                       Float minPrice, Float maxPrice, Integer sortStatus) {

        Page<Product> page = PageHelper.startPage(pageNum, 3);
        List<Product> productList = null;

        //0:默认排序；1：产品销量排序；2：产品星级排序；
        // 3：产品价格升序；4：产品价格降序
        if (sortStatus == null || sortStatus == 0)
            productList = productMapper.selectAllProductOrderByName(keyword, method, days, startCity,
                    feature, minPrice, maxPrice);
        else if (sortStatus == 1)
            productList = productMapper.selectAllProductOrderSales(keyword, method, days, startCity,
                    feature, minPrice, maxPrice);
        else if (sortStatus == 2)
            productList = productMapper.selectAllProductOrderStar(keyword, method, days, startCity,
                    feature, minPrice, maxPrice);
        else if (sortStatus == 3)
            productList = productMapper.selectAllProductOrderByPrice(keyword, method, days, startCity,
                    feature, minPrice, maxPrice);
        else if (sortStatus == 4)
            productList = productMapper.selectAllProductOrderByPriceDesc(keyword, method, days, startCity,
                    feature, minPrice, maxPrice);
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(productList);


        if (productList != null || productList.size() > 0)
            return ServerResponse.getDataSuccess(pageInfo);
        else
            return ServerResponse.getDataFailed("没有查询到数据");
    }

    //获得秒杀列表
    @Override
    public ServerResponse getSecKillList() {

        List<Product> secProductList = new ArrayList<>();
        List<Seckill> seckillList = new ArrayList<>();
        SeckillExample example = new SeckillExample();
        Seckill seckill = seckillMapper.selectByPrimaryKey(1);
        SeckillExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if (new Date().getHours() > 11) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        Date startTime = calendar.getTime();
        criteria.andStartTimeEqualTo(startTime);
        seckillList = seckillMapper.selectByExample(example);

        for (Seckill secKill : seckillList) {

            int proId = secKill.getProductId();
            Product product = productMapper.selectByPrimaryKey(proId);
            //秒杀结束时间
            long endTime = secKill.getEndTime().getTime();
            //当前时间
            long nowTime = new Date().getTime();
            //redis过期时间
            long mapLife = endTime - nowTime;
            int secId = secKill.getId();
            int secNumber = secKill.getSeckillNumber();
            //将秒杀id和商品数量存入redis缓存
            Map<Integer, Integer> secMap = new HashMap<Integer, Integer>();
            secMap.put(secId, secNumber);
            redisTemplate.opsForHash().putAll("recKill", secMap);
//            redisTemplate.expire("recKill",mapLife,TimeUnit.MILLISECONDS);

            secProductList.add(product);
        }
        Map<String, List> dataaMap = new HashMap();
        dataaMap.put("seckillList", seckillList);
        dataaMap.put("secProductList", secProductList);
        if (seckillList != null && seckillList.size() > 0) {
            if (secProductList == null && secProductList.size() == 0) {
                return ServerResponse.getDataSuccess("商品已下架");
            }

            return ServerResponse.getDataSuccess(dataaMap);
        }
        return ServerResponse.getDataFailed("获取数据失败");

    }
}
